<template>
	<view class="management">
		<view class="addressContent" v-if="address">
			<view class="addressItem"  v-for="i in address" :key="i">
			<view class="ItemTitle">
				<view class="ItemName">
					{{i.name}}
				</view>
				<view class="ItemPhone">
					{{i.phone}}
				</view>
			</view>
			<view class="ItemAddress">
				{{i.province+i.city+i.district+i.address}}
			</view>
			<view class="operation">
				<view class="operation_left">
					<view class="radio" :class="i.status==1?1:'radioActive'" @click="radiochange(i)"></view>
					<text>默认地址</text>
				</view>
				<view class="operation_right">
					<view class="right_operation" @click="operation(i)">编辑</view>
					<view class="right_del" @click="del(i)">删除</view>
				</view>
			</view>
		</view>

		</view>
		<view class="addressContent content1" v-else>
			<image src="../static/hezi.png" mode="widthFix"></image>
			<view class="">
				您还未添加任何地址哦，赶快去添加一个吧！
			</view>
		</view>
		<view class="button savebottom">
			<button type="warn" @click="addmanagement">新增地址</button>
		</view>
	</view>
</template>

<script>
	import md5 from '@/common/api/md5.js'
	import {requestUrl} from '@/common/config.js'
	export default {
		data() {
			return {
				address:[],
				access_token:''
			};
		},
		onShow() {
			let time = Math.round(new Date().getTime() / 1000).toString()
			let _this = this
			let Time = ['time', time].join('=')
			this.access_token=JSON.parse(uni.getStorageSync('access_token'))
			let Access_token = ['access_token', this.access_token].join('=')
			let content = ['content=pinet.FeiMao.2017'].join()
			let arr = [content, Time].sort(function(a, b) {
				a - b
			})
			let newarr = md5(arr.join('&'))
			uni.request({
				url: requestUrl+'/app/address/getAllAddress',
				method: "GET",
				data: {
					access_token: JSON.parse(uni.getStorageSync('access_token')),
					time: time,
					sign: newarr,
				},
				success: function(res) {
					console.log(res)
					_this.address=res.data.content
				}
			});
		},
		methods:{
			operation(e){
				uni.setStorage({
				    key: 'e',
				    data: JSON.stringify(e),
				    success: function () {
				    }
				});
				uni.navigateTo({
					url:'/pagesA/management/modify'
				})
			},
			addmanagement(){
				uni.navigateTo({
					url:'/pagesA/management/addmanagement'
				})
			},
			del(e){
				console.log(e)
				let time = Math.round(new Date().getTime() / 1000).toString()
				let _this = this
				let Time = ['time', time].join('=')
				let Access_token = ['access_token', this.access_token].join('=')
				let content = ['content=pinet.FeiMao.2017'].join()
				let arr = [content, Time].sort(function(a, b) {
					a - b
				})
				let newarr = md5(arr.join('&'))
				uni.request({
					url: requestUrl+'/app/address/deleteAddress',
					method: "GET",
					data: {
						access_token: JSON.parse(uni.getStorageSync('access_token')),
						time: time,
						sign: newarr,
						address_id:e.address_id
					},
					success: function(res) {
						uni.showToast({
							title:res.data.message
						})
						if(res.data.errcode==0){
							uni.request({
								url: requestUrl+'/app/address/getAllAddress',
								method: "GET",
								data: {
									access_token: JSON.parse(uni.getStorageSync('access_token')),
									time: time,
									sign: newarr,
								},
								success: function(res) {
									console.log(res)
									_this.address=res.data.content
								}
							});
						}
					}
				});
			},
			radiochange(e){}
		}
	}
</script>

<style lang="less">
	.management{
		height: 100%;
		display: flex;
		flex-direction: column;
		background-color: #F3F4F5;
		.addressContent{
			flex: 1;
			padding-top: 20rpx;
		}
		.content1{
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #AAAAAA;
			image{
				width: 200px;
			}
			view{
				text-align: center;
			}
		}
		.addressItem{
			padding: 30rpx 0 0;
			margin-bottom: 10rpx;
			background: #fff;
			.ItemTitle{
				padding: 0 30rpx;
				display: flex;
				margin-bottom: 16rpx;
				align-items: center;
				height: 34rpx;
				.ItemName{
					font-size: 32rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #111111;
				}
				.ItemPhone{
					margin-left: 22rpx;
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #111111;
				}
			}
			.ItemAddress{
				padding: 0 30rpx 30rpx;
				font-size: 26rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #9DA5A8;
			}
			.operation{
				display: flex;
				justify-content: space-between;
				padding: 30rpx;
				border-top: 1px solid #EEEEEE;
				.operation_left{
					position: relative;
					.radio {
						width: 32rpx;
						height: 32rpx;
						position: absolute;
						top: 8rpx;
						left: 0rpx;
						background: url(@/static/selected%20active.png) 0px 0px;
						background-size: 32rpx 32rpx;
					}
					.radioActive {
						background: url(@/static/selected／on.png) 0px 0px;
						background-size: 32rpx 32rpx;
					}
					text{
						padding-left: 46rpx;
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #111111;
					}
				}
				.operation_right{
					display: flex;
					.right_operation{
						padding-left:40rpx ;
						background: url(/pagesA/static/edit.png) no-repeat 8rpx 3rpx ;
						background-size: 28rpx 30rpx;
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #111111;
					}
					.right_del{
						padding-left:40rpx ;
						background: url(/pagesA/static/Del.png) no-repeat 8rpx 4rpx ;
						background-size: 24rpx 24rpx;
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #111111;
					}
				}
			}
		}
		.button {
			margin: 0 20rpx;
			width: 95%;
			margin: 0 auto;
			padding: 14rpx 0;
			button{
				border-radius: 20px;
				line-height: 80rpx;
			}
		}
		.savebottom {
		padding-bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	}
	}
</style>
